Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
LeetCode 每日一题:2976. Minimum Cost to Convert String I
题目链接: https://leetcode.com/problems/minimum-cost-to-convert-string-i/
难度: Medium
解题过程
思考过程
我需要先获取题目ID 2976的信息。
思考过程
现在我需要分析题目。这是第2976题:"Minimum Cost to Convert String I"。题目要求计算将源字符串转换为目标字符串的最小成本。我们有一系列的转换规则,每个规则指定了将字符 original[i] 转换为 changed[i] 的成本 cost[i]。我们可以执行任意数量的操作,每次操作可以选择一个字符 x 转换为 y,前提是存在这样的规则。我们需要找到从 source 到 target 的最小总成本,如果不可能则返回 -1。
观察约束条件:source 和 target 的长度最多 10^5,original/changed 数组的长度最多 2000。字符只有 26 个小写字母。因此我们需要为每个字符对 (u, v) 计算最小转换成本,即从字符 u 转换为字符 v 的最小成本,允许通过中间字符转换(类似 Floyd-Warshall 算法)。由于最多只有 26 个节点,我们可以预先计算所有字符对之间的最短路径(最小成本)。然后对于 source 和 target 中每个位置 i,如果 source[i] == target[i],则成本为 0。否则我们需要查找从 source[i] 到 target[i] 的最小成本。如果不可达(即距离为无穷大),则返回 -1。否则累加所有位置的成本。
需要注意的细节:
long minimumCost(...),但代码模板中是long long minimumCost(...)。模板中是long long,应该使用long long类型。算法步骤:
时间复杂度:O(26^3 + n + m),其中 n 是 source 长度,m 是规则数量。
空间复杂度:O(26^2) = O(1)。
现在需要生成三个文件:头文件、源文件、测试文件。由于这是一个普通题目(非设计类),应该使用 SolutionBase 模式。根据题目信息,solution_class_name 是 "MinimumCostToConvertStringISolution",solution_class_base 是 "MinimumCostToConvertStringI",test_class_name 是 "MinimumCostToConvertStringITest",namespace 是 "problem_2976"。我需要遵循这些命名。
首先看一下提供的示例 "TwoSum" 的格式。
头文件定义了一个继承自 SolutionBase 的类。Func 是函数类型 std::function<long long(string, string, vector&, vector&, vector&)>?实际上这个问题的函数签名是 long long minimumCost(string source, string target, vector& original, vector& changed, vector& cost)。因此 Func 应该是 std::function<long long(string, string, vector&, vector&, vector&)>。
然后类中定义 public 方法 minimumCost,以及构造函数。构造函数会注册策略。我们可以实现多种策略,但这个问题只有一种明显的算法(Floyd-Warshall),因此我可以只实现一个策略。不过为了展示,我可以实现两种策略:一种是朴素的 Floyd-Warshall,另一种是使用 Dijkstra 算法(每个字符作为源点计算到其他字符的最短路径),但意义不大。还是只实现一个策略即可。
测试文件需要包含多个测试用例,包括题目提供的示例。
现在开始生成代码。
首先,我需要确保在头文件中包含 "leetcode/core.h",并且使用正确的命名空间。
让我编写头文件:
思考过程
现在编译项目验证代码是否正确。
思考过程
很好,编译成功。现在运行测试用例。
思考过程
所有测试通过!很好。
现在我需要输出解题报告,描述思考过程和算法设计。
我会按照要求输出 Markdown 格式的解题报告。
AI 回复
解题报告:LeetCode 2976. Minimum Cost to Convert String I
题目理解
题目要求计算将源字符串
source转换为目标字符串target的最小成本。我们有一系列转换规则,每个规则指定了将字符original[i]转换为changed[i]的成本cost[i]。每次操作可以选择一个字符进行转换,前提是存在相应的转换规则。我们可以执行任意多次操作,最终目标是使source完全变成target。如果无法完成转换,返回 -1。关键点:
算法设计
这是一个典型的图论最短路径问题。我们可以将 26 个字母看作图中的 26 个节点,每条转换规则
original[i] → changed[i]看作一条有向边,权重为cost[i]。问题转化为:对于每个位置 i:
source[i] == target[i],成本为 0source[i]到target[i]的最短路径长度(最小成本)由于只有 26 个节点,我们可以预先计算所有字符对之间的最短路径,然后直接查表计算总成本。
算法一:Floyd-Warshall(推荐)
思路:
dist,对角线为 0(相同字符转换成本为 0),其他为无穷大时间复杂度:
空间复杂度:
算法二:Dijkstra(每个字符作为源点)
思路:
时间复杂度:
空间复杂度:
实现细节
long long类型存储距离,因为单次成本最大为 10⁶,字符串长度最大为 10⁵,总成本可能达到 10¹¹,超出 32 位整数范围。关键点总结
测试用例设计
测试覆盖了以下场景:
所有测试用例均通过,验证了算法的正确性。
✅ 解题完成!代码已通过编译和测试。
本报告由 AI 自动生成,包含完整的思考过程和解题步骤。